import {describe} from 'mocha'
import {getInputs} from '../src/input-check'
import {expect} from 'chai'
import { env } from 'process';
import { mock, restore } from 'sinon';

describe('getInputs', () => {
    let originalEnv: NodeJS.ProcessEnv;

    beforeEach(() => {
        originalEnv = { ...env };
        mock(env);
        env['WORK_SPACE']='sss'
        env['PROJECT_DIR']='sss'
        env['uploadType']='sss'
        env['ARCHIVE_PATH']='sss'
        env['ARTIFACT']='sss'
        env['ARTIFACT']='sss'
        env['ARTIFACT_NAME']='sss'
        env['DATETIME']='sss'
        env['stepIdentifier']='sss'
        env['FILE_PARENT_ID']='sss'
        env['ENGINE_GLOBAL_PARAM_ORGANIZATION_ID']='sss'

    });

    afterEach(() => {
        restore();
        Object.assign(env, originalEnv);
    });

    it('should throw an error if WORK_SPACE is not defined', async () => {
        delete env['WORK_SPACE'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'WORK_SPACE\'');
        }
    });

    it('should throw an error if PROJECT_DIR is not defined', async () => {
        delete env['PROJECT_DIR'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'PROJECT_DIR\'');
        }
    });

    it('should throw an error if uploadType is not defined', async () => {
        delete env['uploadType'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'uploadType\'');
        }
    });

    it('should throw an error if ARCHIVE_PATH is not defined', async () => {
        delete env['ARCHIVE_PATH'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'ARCHIVE_PATH\'');
        }
    });

    it('should throw an error if ARTIFACT is not defined', async () => {
        delete env['ARTIFACT'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'ARTIFACT\'');
        }
    });

    it('should throw an error if ARTIFACT contains spaces', async () => {
        env['ARTIFACT'] = 'artifact name with spaces';
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('invalid artifact name');
        }
    });

    it('should throw an error if ARTIFACT_NAME is not defined', async () => {
        delete env['ARTIFACT_NAME'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'ARTIFACT_NAME\'');
        }
    });

    it('should throw an error if DATETIME is not defined', async () => {
        delete env['DATETIME'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'DATETIME\'');
        }
    });

    it('should throw an error if stepIdentifier is not defined', async () => {
        delete env['stepIdentifier'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'stepIdentifier\'');
        }
    });

    it('should throw an error if FILE_PARENT_ID is not defined', async () => {
        delete env['FILE_PARENT_ID'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'FILE_PARENT_ID\'');
        }
    });

    it('should throw an error if ENGINE_GLOBAL_PARAM_ORGANIZATION_ID is not defined', async () => {
        delete env['ENGINE_GLOBAL_PARAM_ORGANIZATION_ID'];
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('missing params  \'ENGINE_GLOBAL_PARAM_ORGANIZATION_ID\'');
        }
    });

    it('should set default values for ARTIFACT_VERSION and REPO_ID', async () => {
        delete env['ARTIFACT_VERSION'];
        delete env['REPO_ID'];

        const inputs = await getInputs();
        expect(inputs.artifactVersion).to.equal(env['DATETIME']);
        expect(inputs.repoId).to.equal('flow_generic_repo');
    });

    it('should set default value for GIT_IGNORE', async () => {
        delete env['GIT_IGNORE'];
        const inputs = await getInputs();
        expect(inputs.gitIgnore).to.equal('false');
    });

    it('should throw an error if ARTIFACT_VERSION contains spaces', async () => {
        env['ARTIFACT_VERSION'] = 'version with spaces';
        try {
            await getInputs()
            expect.fail('Expected an error to be thrown');

        }catch (error){
            // @ts-ignore
            expect(error.message).to.contain('invalid artifact version');

        }
    });
});
